package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.StgyException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.FeeKey;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.model.entity.FeeValue;
import com.atguigu.lease.web.admin.service.FeeValueService;
import com.atguigu.lease.web.admin.mapper.FeeValueMapper;
import org.springframework.stereotype.Service;

/**
 * @author gqs
 * @description 针对表【fee_value(杂项费用值表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class FeeValueServiceImpl extends ServiceImpl<FeeValueMapper, FeeValue>
    implements FeeValueService {

  @Override
  public boolean saveOrUpdateFeeValue(FeeValue feeValue) {
    Long id = feeValue.getId() == null ? -1 : feeValue.getId();

    FeeValue dbFeeValue =
        this.getOne(
            Wrappers.lambdaQuery(FeeValue.class)
                .eq(FeeValue::getName, feeValue.getName())
                .last("limit 1"));
    if (dbFeeValue != null && dbFeeValue.getId().equals(id)) {
      throw new StgyException(ResultCodeEnum.FEE_VALUE_EXISTS_ERROR);
    }
    return this.saveOrUpdate(feeValue);
  }
}
